package com.fadis.data;


import com.fadis.FadisException;

import java.util.*;

public class FStringHash {

    /**
     * @param map
     * @param field
     * @param value
     * @return 1表示新建，0表示覆盖
     */
    public static int hset(Map<String, String> map, String field, String value) {
        boolean contains = map.containsKey(field);
        map.put(field, value);
        return contains ? 0 : 1;
    }

//    public static Long hset(Map<String, String> map, Map<String, String> hash) {
//        this.checkIsInMultiOrPipeline();
//        this.client.hset(key, hash);
//        return this.client.getIntegerReply();
//    }

    public static String hget(Map<String, String> map, String field) {
       return map == null ? null : map.get(field);
    }

    /**
     * 不存在则设置
     * @param map
     * @param field
     * @param value
     * @return 1表示新建成功，0表示已存在
     */
    public static int hsetnx(Map<String, String> map, String field, String value) {
        if(map.containsKey(field)){
            return 0;
        }
        map.put(field, value);
        return 1;
    }

    public static String hmset(Map<String, String> map, Map<String, String> hash) {
        if(map == null) return "OK";
        for (String k: hash.keySet()){
            map.put(k, hash.get(k));
        }
        return "OK";
    }

    /**
     * 注意，总是返回一个size = fields.length的数组
     * 如果map不存在，则返回list的元素都是null
     * @param map
     * @param fields
     * @return
     */
    public static List<String> hmget(Map<String, String> map, String... fields) {
        List<String> list = new ArrayList<>();
        if(map == null){
            for (int i = 0; i < fields.length; i++) {
                list.add(null);
            }
            return list;
        }

        for (int i = 0; i < fields.length; i++) {
            list.add(map.get(fields[i]));
        }
        return list;
    }

    /**
     * field无则新建，并设置为0
     * @param map
     * @param field
     * @param value
     * @return field最新的值
     */
    public static long hincrBy(Map<String, String> map, String field, long value) {
        if(map == null) return 0L;
        String v = map.get(field);
        if(v == null) v = "0";

        long lv = 0;
        try {
            lv = Long.parseLong(v);
        }catch (Exception e){
            throw new FadisException(2, field + "==>" + v + " is not long value");
        }

        lv += value;
        map.put(field, lv + "");
        return lv;
    }

    public static double hincrByFloat(Map<String, String> map, String field, double value) {
        if(map == null) return 0;
        String v = map.get(field);
        if(v == null) v = "0";

        double lv = 0;
        try {
            lv = Double.parseDouble(v);
        }catch (Exception e){
            throw new FadisException(2, field + "==>" + v + " is not double value");
        }

        lv += value;
        map.put(field, lv + "");
        return lv;
    }

    public static Boolean hexists(Map<String, String> map, String field) {
        if(map == null) return false;
        return map.containsKey(field);
    }

    /**
     *
     * @param map
     * @param fields
     * @return 删除成功的数量（存在且被删除）
     */
    public static int hdel(Map<String, String> map, String... fields) {
        int count = 0;

        if(map == null) return count;
        for (String k: fields){
            if(map.containsKey(k)) {
                count++;
                map.remove(k);
            }
        }

        return count;
    }

    /**
     *
     * @param map
     * @return key的数量，无则0
     */
    public static int hlen(Map<String, String> map) {
        if(map == null) return 0;
        return map.size();
    }

    public static int hstrlen(Map<String, String> map, String field) {
        if(map == null) return 0;
        if(!map.containsKey(field)) return 0;
        String s = map.get(field);
        return s == null ? 0 : s.length();
    }

    public static Set<String> hkeys(Map<String, String> map) {
        Set<String> set = new HashSet<>();
        if(map == null) return set;
        for (String k: map.keySet()){
            set.add(k);
        }
        return set;
    }

    public static List<String> hvals(Map<String, String> map) {
        List<String> set = new ArrayList<>();
        if(map == null) return set;
        for (String v: map.values()){
            set.add(v);
        }
        return set;
    }

    public static Map<String, String> hgetAll(Map<String, String> map) {
        Map<String, String> set = new HashMap<>();
        if(map == null) return set;
        for (String k: map.keySet()){
            set.put(k, map.get(k));
        }
        return set;
    }

}
